package com.example.hospital.api.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateRange;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.example.hospital.api.common.R;
import com.example.hospital.api.controller.form.DeleteWorkPlanForm;
import com.example.hospital.api.controller.form.InsertWorkPlanForm;
import com.example.hospital.api.controller.form.SearchWorkPlanInRangeForm;
import com.example.hospital.api.service.MedicalDeptSubWorkPlanService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Map;

/**
 * @author Baiken
 * @version 1.0
 * @description: 出诊表
 * @date 2024/9/9 下午7:08
 */


@RestController
@RequestMapping("/medical/dept/sub/work_plan")
public class MedicalDeptSubWorkPlanController {
    @Resource
    private MedicalDeptSubWorkPlanService  doctorWorkPlanService;

    @PostMapping("/searchWorkPlanInRange")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT","SCHEDULE:SELECT"},mode = SaMode.OR)
    public R searchWorkPlanInRange(@RequestBody SearchWorkPlanInRangeForm form){
        Map param = BeanUtil.beanToMap(form);
        DateRange range = DateUtil.range(new DateTime(form.getStartDate()),
                new DateTime(form.getEndDate()), DateField.DAY_OF_MONTH);
        ArrayList dataList = new ArrayList();
        range.forEach(one ->{
            dataList.add(one.toDateStr());
        });
        JSONArray array = doctorWorkPlanService.searchWorkPlanInRange(param, dataList);
        dataList.clear();
        range.reset();
        range.forEach(one -> {
            dataList.add(one.toString("MM月dd日")+ "(" + one.dayOfWeekEnum().toChinese()+")");
        });
        return R.ok().put("result",array).put("dateList",dataList);
    }


    @PostMapping("/insert")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT","SCHEDULE:INSERT"},mode = SaMode.OR)
    public R insert(@RequestBody InsertWorkPlanForm form){
        Map param = BeanUtil.beanToMap(form);
        doctorWorkPlanService.insert(param);
        return R.ok().put("result","");
    }

    @PostMapping("/deleteWorkPlan")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT","SCHEDULE:DELETE"},mode = SaMode.OR)
    public R deleteWorkPlan(@RequestBody @Valid DeleteWorkPlanForm form){
        doctorWorkPlanService.deleteWorkPlan(form.getWorkPlanId());
        return R.ok();
    }
}
